package com.hippo.core.ui.compose.home.add

import LoadingDialog
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.hippo.core.R
import com.hippo.core.core.ext.click
import com.hippo.core.core.ext.paddingBody
import com.hippo.core.tuya.TuYaManager
import com.hippo.core.ui.components.BottomSheetDialog
import com.hippo.core.ui.components.GifImage
import com.hippo.core.ui.components.StateImage
import com.hippo.core.ui.theme.HippoTheme
import com.hippo.core.utils.AppUtil
import com.thingclips.smart.android.ble.api.ScanDeviceBean
import com.thingclips.smart.home.sdk.ThingHomeSdk
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@Composable
fun ScanDeviceDialog(
    onDismissRequest: () -> Unit,
    reScan: () -> Unit,
    onFind: (ScanDeviceBean) -> Unit,
    toHelp: () -> Unit
) {


    DisposableEffect(Unit) {
        TuYaManager.startLeScan {
            onFind(it)
        }
        onDispose {
            TuYaManager.stopScan()
        }
    }

    BottomSheetDialog(
        title = stringResource(R.string.home_6),
        onDismissRequest = onDismissRequest,
        skipPartiallyExpanded = true,
    ) {
        ScanDeviceContent(toHelp)
    }
}


@Composable
private fun ScanDeviceContent(toHelp: () -> Unit) {

    val bluetoothOpen = remember { mutableStateOf(false) }
    val showLoad = remember { mutableStateOf(false) }

    val scope = rememberCoroutineScope()

    LaunchedEffect(Unit) {
        bluetoothOpen.value = AppUtil.isBluetoothEnabled()
    }

    if(showLoad.value){
        LoadingDialog {
            showLoad.value = false
        }
    }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .paddingBody()
    ) {


        Row {
            Text(
                text = stringResource(R.string.home_7),
                color = Color.Gray,
                modifier = Modifier.weight(1f)
            )

            Image(
                painter = painterResource(R.mipmap.refresh),
                contentDescription = "Back",
                modifier = Modifier
                    .size(18.dp)
                    .click {
                        showLoad.value = true
                        scope.launch {
                            delay(3000)
                            showLoad.value = false
                        }
                    }
            )

        }

        Spacer(modifier = Modifier.padding(top = 16.dp))

        Text(
            text = stringResource(R.string.home_8),
            color = Color.Black,
            fontSize = 25.sp,
        )

        Spacer(modifier = Modifier.padding(top = 16.dp))

        Column(
            modifier = Modifier
                .fillMaxWidth()
                .background(
                    color = Color.White,
                    shape = RoundedCornerShape(8.dp)
                )
                .border(1.dp, Color(0xFFE5E5E5), shape = RoundedCornerShape(8.dp))
                .padding(16.dp)
        ) {
            Row {

                Text(
                    text = stringResource(R.string.home_9),
                    color = Color.Black,
                    fontSize = 16.sp,
                    modifier = Modifier.weight(1f)
                )

                StateImage(
                    checked = bluetoothOpen.value,
                    defaultImageRes = R.mipmap.blueth_n,
                    checkedImageRes = R.mipmap.blueth_s,
                    modifier = Modifier
                        .size(18.dp)
                        .click {

                        }
                )

            }

            Text(
                text = stringResource(R.string.home_10),
                color = Color.Gray,
                fontSize = 12.sp,
            )

        }


        Column(
            modifier = Modifier
                .fillMaxWidth()
        ) {

            Spacer(modifier = Modifier.weight(1F))

            GifImage(
                id = R.drawable.scan_wifi,
                modifier = Modifier
                    .fillMaxWidth(0.8F)
                    .align(Alignment.CenterHorizontally)
            )

            Spacer(modifier = Modifier.weight(1F))

            Text(
                text = stringResource(R.string.home_11),
                color = MaterialTheme.colorScheme.primary,
                textDecoration = TextDecoration.Underline,
                fontSize = 14.sp,
                modifier = Modifier.align(Alignment.CenterHorizontally)
                    .click {
                        toHelp()
                    }
            )

            Spacer(modifier = Modifier.weight(1F))


        }

    }

}


@Preview(showBackground = true)
@Composable
private fun ScanDeviceContentPreview() {
    HippoTheme {
        ScanDeviceContent(
            toHelp = {}
        )
    }
}